﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Net.Mail;
using System.Text;
using System.Security.Cryptography;

/// <summary>
///PageBase 的摘要说明
/// </summary>
public class WebFormBase : Page
{
    //protected string GetMD5(string sDataIn)
    //{
    //    using (MD5 md5 = new MD5CryptoServiceProvider())
    //    {
    //        byte[] bytValue = Encoding.UTF8.GetBytes(sDataIn);
    //        byte[] bytHash = md5.ComputeHash(bytValue);
    //        md5.Clear();
    //        StringBuilder sb = new StringBuilder();
    //        for (int i = 0; i < bytHash.Length; i++)
    //        {
    //            sb.Append(bytHash[i].ToString("x2").PadLeft(2, '0'));
    //        }
    //        return sb.ToString().ToUpper();
    //    }
    //}

    /// <summary>
    /// 
    /// </summary>
    /// <param name="message"></param>
    protected void Alert(string message)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "key", "<script>alert('" + message + "');</script>");
        //string s = "<Script language='JavaScript'>\r\n alert('" + message + "');</Script>";
        //HttpContext.Current.Response.Write(s);
    }

    protected void Alert(string message, string methodName)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "key", "<script>" + methodName + "();alert('" + message + "');</script>");
        //string s = "<Script language='JavaScript'>\r\n alert('" + message + "');closePanel();</Script>";
        //HttpContext.Current.Response.Write(s);
    }

    protected void Alert(string message, string methodName, string methodName2)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "key", "<script>" + methodName + "();alert('" + message + "');" + methodName2 + "();</script>");
        //string s = "<Script language='JavaScript'>\r\n alert('" + message + "');closePanel();</Script>";
        //HttpContext.Current.Response.Write(s);
    }

    protected void RedirectNewURL(string url)
    {
        StringBuilder builder = new StringBuilder();
        builder.Append("<script language=\"javascript\"> \n");
        builder.Append("window.location.href=\"" + url.Trim() + "\";\n");
        builder.Append("</script>");
        HttpContext.Current.Response.Write(builder.ToString());
    }

    protected void AlertRedirect(string message, string url)
    {
        StringBuilder builder = new StringBuilder();
        builder.Append("<script language=\"javascript\"> \n");
        if (!string.IsNullOrEmpty(message))
        builder.AppendFormat("alert(\"{0}\");", message);
        builder.AppendFormat("window.location.href=\"{0}\"", url);
        builder.Append("</script>");
        HttpContext.Current.Response.Write(builder.ToString());
        //HttpContext.Current.Response.End();
        //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "key", builder.ToString());
        //string s = "<Script language='JavaScript'>\r\n alert('" + message + "');closePanel();</Script>";
        //HttpContext.Current.Response.Write(s);
    }
    protected void AlertRedirectStartup(string message, string url)
    {
        StringBuilder builder = new StringBuilder();
        builder.Append("<script language=\"javascript\"> \n");
        if (!string.IsNullOrEmpty(message))
            builder.AppendFormat("alert(\"{0}\");", message);
        builder.AppendFormat("window.location.href=\"{0}\"", url);
        builder.Append("</script>");
        Page.ClientScript.RegisterStartupScript(this.GetType(), "key1", builder.ToString());
    }
    protected void AlertRedirectBlock(string message, string url)
    {
        StringBuilder builder = new StringBuilder();
        builder.Append("<script language=\"javascript\"> \n");
        if (!string.IsNullOrEmpty(message))
            builder.AppendFormat("alert(\"{0}\");", message);
        builder.AppendFormat("window.location.href=\"{0}\"", url);
        builder.Append("</script>");
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "key1", builder.ToString());
    }
    protected void RedirectNewRedirectMethodURL(string url)
    {
        StringBuilder builder = new StringBuilder();
        builder.Append("<script language=\"javascript\"> \n");
        builder.Append("window.parent.Redirect(\"" + url.Trim() + "\")");
        builder.Append("</script>");
        HttpContext.Current.Response.Write(builder.ToString());
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="strSmtpServer">第一个参数如果是163邮箱就写smtp.163.com</param>
    /// <param name="strFrom">第二个参数发件人的帐号</param>
    /// <param name="strFromPass">第三个参数发件人密码</param>
    /// <param name="strto">第四个参数收件人帐号</param>
    /// <param name="strSubject">第五个参数主题</param>
    /// <param name="strBody">第六个参数内容.</param>
    protected void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
    {
        System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        System.Net.Mail.MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
        message.BodyEncoding = System.Text.Encoding.UTF8;
        message.IsBodyHtml = true;
        client.Send(message);
    }

    protected byte[] HexToByte(string hexString)
    {
        hexString = hexString.Replace(" ", "");
        if ((hexString.Length % 2) != 0)
            hexString += " ";
        byte[] returnBytes = new byte[hexString.Length / 2];
        for (int i = 0; i < returnBytes.Length; i++)
            returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
        return returnBytes;
    }
    protected string ByteToHexX2(byte[] bytes)
    {
        string returnstr = "";
        if (bytes != null)
            foreach (byte b in bytes)
                returnstr += b.ToString("X2");

        return returnstr;
    }
    /// <summary>
    /// 显示在网面的上,注意此方法一定是引入jQuery
    /// </summary>
    /// <param name="id">显示内容的ID</param>
    /// <param name="msg">显示的内容</param>
    protected void ShowMethod(string method, string strMsg)
    {
        StringBuilder builder = new StringBuilder();
        builder.Append("<script language='javascript' defer>");
        builder.Append(string.Format("{0}({1});", new object[] { method, strMsg }));
        builder.Append("</script>");
        ClientScript.RegisterClientScriptBlock(typeof(Page), "message", builder.ToString());
    }
}